#pragma once

#include <iostream>
#include <assert.h>
using namespace std;

namespace ywx {
	template <typename T>
	class vector {
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{}

		vector(size_t n, const T& val)
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			resize(n, val);
		}

		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}

		vector(vector<T>& v)
		{
			v._start = new T[T.capacity()];
			if (v._start)
			{
				//memcpy(tmp, _start, sizeof(T) * size());
				for (int i = 0; i < size(); i++)
				{
					_start[i] = v._start[i];
				}
			}
			_finish = v._finish;
			_end_of_storage = v._end_of_storage;
		}

		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finish = _end_of_storage = nullptr;
			}
		}

		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		void swap(const vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				T* tmp = new T[n];
				size_t oldSize = size();
				if (_start)
				{
					//memcpy(tmp, _start, sizeof(T) * size());
					for (int i = 0; i < size(); i++)
					{
						tmp[i] = _start[i];
					}
					delete _start;
				}
				_start = tmp;
				_finish = tmp + oldSize;
				_end_of_storage = tmp + n;
			}
		}

		void resize(size_t n, const T& val = T())
		{
			if (n < _size)
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);

				whlie(_finish != _start + n)
				{
					*_finish = val;
					++_finish; 
				}
			}
		}

		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				size_t newCapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newCapacity);
			}

			*_finish = x;
			++_finish;
		}

		void pop_back()
		{
			if (size())
			{
				--_finish;
			}
		}

		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= _start && pos < _finish);

			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				size_t newCapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newCapacity);
				pos = _start + len;
			}

			iterator end = _finish;
			while (end >= pos)
			{
				*end = *(end - 1);
				--end;
			}
			*pos = x;
			_finish++;
			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos < _finish);
			iterator it = pos;
			while (it < _finish)
			{
				*it = *(it + 1);
				++it;
			}
			--_finish;
			return pos;
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}

		const T& operator[](size_t pos) const
		{
			assert(pos < size());
			return _start[pos];
		}

		vector<T>& operator=(vector<T>& v)
		{
			v._start = new T[T.capacity()];
			if (v._start)
			{
				//memcpy(tmp, _start, sizeof(T) * size());
				for (int i = 0; i < size(); i++)
				{
					_start[i] = v._start[i];
				}
			}
			_finish = v._finish;
			_end_of_storage = v._end_of_storage;
			return *this;
		}
		
	private:
		iterator _start;
		iterator _finish;
		iterator _end_of_storage;
	};
}